package com.db.nio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class NetworkCopyIOServer extends AbstractCopy {
    public static void main(String[] args) {
        System.out.println("server started");

        try (ServerSocket serverSocket = new ServerSocket(SERVER_PORT, SERVER_BACKLOG)) {
            System.out.println("connection started");

            try (Socket socket = serverSocket.accept();
                 InputStream inputStream = socket.getInputStream();
                 OutputStream outputStream = new FileOutputStream(FILE_2)) {

                System.out.println("client connected");

                long startTime = System.currentTimeMillis();

                byte[] data = new byte[BUFFER_SIZE];
                while ((inputStream.read(data)) != -1) {
                    outputStream.write(data);
                }

                long time = (System.currentTimeMillis() - startTime);

                System.out.println("success");
                System.out.println(time + "ms");

            } catch (IOException e) {
                System.out.println("input output exception");
            }
        } catch (IOException e) {
            System.out.println("input output exception");
        }
    }
}
